package com.ruoyi.lock.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.lock.domain.*;
import com.ruoyi.lock.service.*;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lock/menu")
public class MenuController extends BaseController {
    @Autowired
    private IALogsService aLogsService;

    @Autowired
    private IAWaringInfoService waringInfoService;

    @Autowired
    private IATenantuserService tenantuserService;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private IADeviceService iaDeviceService;
    @Autowired
    private IALogsService logsService;
    @Autowired
    private IATenantkeyService aTenantkeyService;
    @Autowired
    private IAOpenlockService aOpenlockService;
    @Autowired
    private IABuildHomeService aBuildHomeService;

    /**
     * 查询开门方式
     */
    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/lockFangShi")
    public AjaxResult lockFangShi(ALogs aLogs)
    {
        List<JSONObject> aLogs1 = aLogsService.selectCountLogsLock(aLogs);
        return AjaxResult.success(aLogs1);
    }

    /**
     * 查询设备状态
     */
//    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/smartlocksStatus")
    public AjaxResult smartlocksStatus(ADevice device)
    {
        return AjaxResult.success(iaDeviceService.findDeviceStatus(device));
    }

    /**
     * 查询钥匙到期状态统计
     */
    @PreAuthorize("@ss.hasPermi('lock:tenantkey:list')")
    @GetMapping("/keyExpiryStatus")
    public AjaxResult keyExpiryStatus(ATenantkey aTenantkey)
    {
        JSONObject result = aTenantkeyService.selectKeyExpiryStatusCount(aTenantkey);
        return AjaxResult.success(result);
    }
    
    /**
     * 查询房间统计信息
     */
    @PreAuthorize("@ss.hasPermi('lock:home:list')")
    @GetMapping("/roomStatistics")
    public AjaxResult roomStatistics(ABuildHome aBuildHome)
    {
        JSONObject result = aBuildHomeService.selectRoomStatistics(aBuildHome);
        return AjaxResult.success(result);
    }

    /**
     * 告警
     */
//    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/gaoJinCount")
    public AjaxResult gaoJinCount(AWaringInfo aWaringInfo)
    {
        JSONObject smartlocks = waringInfoService.gaoJinCount(aWaringInfo);
        return AjaxResult.success(smartlocks);
    }

    /**
     * 查询无居住
     */
//    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/wujuzhuCount")
    public TableDataInfo wujuzhuCount(ALogs aLogs)
    {
        startPage();
        return getDataTable( aLogsService.devicesNoLockActivity24h(aLogs));
    }
    
    /**
     * 查询涉嫌转租15天无开门记录的房间
     */
    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/noOpenLock15Days")
    public TableDataInfo noOpenLock15Days(AOpenlock aOpenlock)
    {
        startPage();
        List<JSONObject> list = aOpenlockService.selectNoOpenLock15Days(aOpenlock);
        return getDataTable(list);
    }

    /**
     * 男女分布
     */
//    @PreAuthorize("@ss.hasPermi('lock:logs:list')")
    @GetMapping("/sexFenBu")
    public AjaxResult sexFenBu(ATenantuser aTenant)
    {
        // 确保只统计flg_delete=1的记录
        if (aTenant.getFlgDelete() == null) {
            aTenant.setFlgDelete("1");
        }
        JSONObject smartlocks = tenantuserService.selectSexCount(aTenant);
        return AjaxResult.success(smartlocks);
    }



    /**
     * 查询开门方式饼图数据
     */
    @GetMapping("/lockFangShiPie")
    public AjaxResult lockFangShiPie(AOpenlock aOpenlock)
    {
        JSONObject result = aOpenlockService.selectOpenLockTypePieData(aOpenlock);
        return AjaxResult.success(result);
    }

    /**
     * 查询设备电量分布
     */
//    @PreAuthorize("@ss.hasPermi('lock:device:list')")
    @GetMapping("/deviceBatteryDistribution")
    public AjaxResult deviceBatteryDistribution(ADevice device)
    {
        return AjaxResult.success(iaDeviceService.findDeviceBatteryDistribution(device));
    }
}
